package com.liuxu.topnews.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.liuxu.topnews.Manager.ActionManager;
import com.liuxu.topnews.bean.App;
import com.liuxu.topnews.function.Read;
import com.liuxu.topnews.utils.LogUtil;
import com.liuxu.topnews.utils.MultiUtils;
import com.liuxu.topnews.utils.SettingUtils;
import com.liuxu.topnews.utils.ShareUtil;
import com.liuxu.topnews.utils.ToastUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Random;

import static com.liuxu.topnews.MyApplication.globalapp;
import static com.liuxu.topnews.MyApplication.globalapps;
import static com.liuxu.topnews.MyApplication.permission;
import static com.liuxu.topnews.MyApplication.random_read;

public class ReadService extends Service {
    private static ReadService Instance;
    public boolean choice[];
    public Thread mythread;
    public String fuction;
    public List<App> readapps;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new Mybinder();
    }
    public boolean onUnBind(Intent intent) {
        return super.onUnbind(intent);
    }

    public void onCreate() {
        super.onCreate();
        Instance=ReadService.this;
        setapps();
        LogUtil.getInstance().show("Service", "onCreate");

    }

    public static ReadService getInstance() {
        return Instance;
    }
    public void onDestroy() {
        super.onDestroy();
        LogUtil.getInstance().show("Service", "onDestroy");

    }


    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("Service", "onStartCommand");
        final int index=intent.getIntExtra("index",0);
        mythread=null;
          mythread=new Thread(new Runnable() {
            @Override
            public void run() {
                Read reader = new Read();
                reader.actionManager = ActionManager.getInstance();
                boolean rember=true;
                if (globalapps ==null){
                    setapps();
                    Log.v("初始化", "从配置文件设置APP");
                }else {
                    Log.v("初始化", "获取到应用APP");
                }
                while (permission) {
                    Log.v("阅读线程", "开始运行");
                    for (int i = 0; i < globalapps.size(); i++) {
                        if (i<index&&rember){
                            i=index-1;
                            rember=false;
                        }
                        if (random_read){
                            i=new Random().nextInt(globalapps.size());
                        }
                        globalapp = globalapps.get(i);
                        if (globalapp.IsCheck&& globalapp.IsInstall&&ShareUtil.getInstance().AppReadCountGet(globalapp)< globalapp.max_read) {
                            fuction= globalapp.ename;
                            reader.init();
                            try {
                                reader.getClass().getDeclaredMethod(fuction).invoke(reader);
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            } catch (InvocationTargetException e) {
                                e.printStackTrace();
                                if (!permission) {
                                    LogUtil.getInstance().show("停止运行");
                                    ToastUtils.getInstance().shownow("停止运行");
                                    break;
                                }
                            } catch (NoSuchMethodException e) {
                                e.printStackTrace();
                            }
                        } else {
                            if (!globalapp.IsInstall){
                                LogUtil.getInstance().show("未安装");
                                ToastUtils.getInstance().shownow("未安装,跳过本平台");
                            }else if (ShareUtil.getInstance().AppReadCountGet(globalapp)>globalapp.max_read){
                                LogUtil.getInstance().show("阅读超限");
                                ToastUtils.getInstance().shownow("阅读超限,跳过本平台");
                            }
                        }

                    }
                }
                ToastUtils.getInstance().shownow("停止运行");
                Log.v("运行完毕","完毕");
            }
        });
          mythread.start();
        return super.onStartCommand(intent, flags, startId);
    }

    class Mybinder extends Binder {
    }

    public void setapps(){
        readapps=new MultiUtils().initAppData(true);
    }

    public void stop(){
        mythread=null;
        this.stopSelf();
//        if (mythread!=null){
//            mythread.isInterrupted();
//            this.stop();
//        }else {
//            ToastUtils.getInstance().shownow("阅读未运行");
//        }
    }
}